/*!
 * jRaiser Module Development Kit
 * Utility functions (2013-01-15T15:20:31+0800)
 * http://jraiser.org/ | Released under MIT license
 */

// 把源对象的属性扩展到目标对象
var extend = function(target) {
	if (target == null) { throw new Error('target cannot be null'); }

	var i = 0, len = arguments.length, p, src;
	while (++i < len) {
		src = arguments[i];
		if (src != null) {
			for (p in src) { target[p] = src[p]; }
		}
	}

	return target;
};

// 在目标字符串中查找子字符串（大小写不敏感）
var iIndexOf = function(target, str) {
	return target.toLowerCase().indexOf( str.toLowerCase() );
};


extend(exports, {
	extend: extend,
	
	// 根据值去重复
	literalUnique: function(arr) {
		var ret = [ ], temp = { }, elt;
		for (var i = 0; i < arr.length; i++) {
			elt = arr[i];
			if (!temp[elt]) {
				temp[elt] = true;
				ret.push(elt);
			}
		}

		return ret;
	},

	// 获取对象的所有属性名
	keys: function(obj) {
		var result = [ ];
		for (var i in obj) {
			result.push(i);
		}
		result.sort();
		return result;
	},

	// 移除字符串前后的花括号
	trimBrace: function(str) {
		return str.replace(/^\{+/, '').replace(/\}+$/, '').trim();
	},

	// 判断指定路径是否URL
	isURL: function(path) { return /^(?:[a-z]+):\/+/i.test(path); },

	iIndexOf: iIndexOf,

	// 字符串替换（大小写不敏感）
	iReplace: function(target, str, replacement) {
		var i = iIndexOf(target, str);
		return i === -1 ? target :
			( target.substr(0, i) + replacement + target.substr(i + str.length) );
	}
});